//
//  KnowCommentVC.m
//  liangyi
//
//  Created by 张水 on 17/2/21.
//  Copyright © 2017年 yswh. All rights reserved.
//

#import "DynamicTrainCell.h"
#import "BLSearchBarTool.h"
#import "IWTextView.h"
#import "DynamicTrainModel.h"
#import "KnowCommentVC.h"
@interface KnowCommentVC ()<UITableViewDelegate,UITableViewDataSource,UITextViewDelegate>

@property(nonatomic,strong) UITableView *tableView;
@property(nonatomic,assign) int pageNo;
@property(nonatomic,strong) BLSearchBarTool *searchBar;
@property(nonatomic,strong) IWTextView *textView;
@property(nonatomic,strong) NSMutableArray *dataArr;

@end

@implementation KnowCommentVC

static NSString *DynamicCellID = @"DynamicTrainCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArr = [NSMutableArray array];
    self.pageNo = 1;
    self.title =  @"用户评论";
    [self initUI];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self loadCommitList];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
-(void)initUI{
    
    self.tableView  = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 50) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.scrollEnabled = YES;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[DynamicTrainCell class] forCellReuseIdentifier:DynamicCellID];
    [CommonMethod hiddleExtendCellFromTableview:self.tableView];
    [self.view addSubview:self.tableView];
    
    self.tableView.delegate = self;
    self.searchBar = [[BLSearchBarTool alloc]init];
    self.searchBar.frame = CGRectMake(0,[UIScreen mainScreen].bounds.size.height-50, KSCREEN_WIDTH, 50);
    self.searchBar.textView.delegate = self;
    self.textView = self.searchBar.textView;
    self.textView.placeholder = @"说点什么吧";
    
    [self.searchBar.sendBtn addTarget:self action:@selector(sendBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.searchBar];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DynamicTrainCell *dynamicCell = [tableView dequeueReusableCellWithIdentifier:DynamicCellID forIndexPath:indexPath];
    if (!dynamicCell){
        dynamicCell = [dynamicCell initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DynamicCellID];
    }
    dynamicCell.selectionStyle = UITableViewCellSeparatorStyleNone;
    dynamicCell.model = self.dataArr[indexPath.section];
    dynamicCell.userSectionLabel.hidden = YES;
    dynamicCell.backView.hidden = YES;
    dynamicCell.imgView.hidden = YES;
    return dynamicCell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%ld",(long)indexPath.row);
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *UIview = [[UIView alloc]init];
    UIview.backgroundColor = [UIColor ColorWithHex:@"#ecf1f2"];
    return  UIview;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *UIview = [[UIView alloc]init];
    UIview.backgroundColor = [UIColor ColorWithHex:@"#ecf1f2"];
    return  UIview;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 1;
}


- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.view endEditing:YES];
}

- (void)sendBtnDidClick:(UIButton *)sendBtn{
    NSLog(@"%@",self.textView.text);
    NSString *url = [BASEURL  stringByAppendingString:KURL_knowledgeComment];
    NSDictionary *dict = @{@"knowId":self.knowId,
                           @"content":self.textView.text};
    
    NSString *jsonString = [NSString dictionaryToJson:dict];
    NSString *AESString = [NSString AES128Encrypt:jsonString];
    NSDictionary *parameters = @{@"parameters":AESString,
                                 @"login":[NSString  AES128Encrypt:[NSString stringWithFormat:@"%@@@%@",[[NSUserDefaults standardUserDefaults]objectForKey:@"userid"],ApplicationDelegate.deviceID]]};
    NSLog(@"%@",dict);
    NSLog(@"%@",parameters);
    
    [AppNetwork requestWithPath:url parameters:parameters  success:^(id responseObject) {
        NSLog(@"%@",responseObject);
        NetworkModel *network = [NetworkModel mj_objectWithKeyValues:responseObject];
        NSInteger code = network.code.integerValue;
        if (code == 0){
            [CustomProgressHUD showSuccessWithStatus:@"评论成功"];
            [self loadCommitList];
        }else{
            [CustomProgressHUD showErrorWithStatus:network.errorDescription];
        }
    } failure:^(id error) {
        [CustomProgressHUD showErrorWithStatus:k_NetWorkStatus_Error];
    }];
    
    self.textView.text = nil;
    
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

-(void)loadCommitList{
    NSString *url = [BASEURL  stringByAppendingString:KURL_knowledgeList];
    NSDictionary *dict = @{@"knowId":self.knowId,
                           @"pageNo":[NSString stringWithFormat:@"%d",self.pageNo]};
    NSString *jsonString = [NSString dictionaryToJson:dict];
    NSString *AESString = [NSString AES128Encrypt:jsonString];
    NSDictionary *parameters = @{@"parameters":AESString,
                                 @"login":[NSString  AES128Encrypt:[NSString stringWithFormat:@"%@@@%@",[[NSUserDefaults standardUserDefaults]objectForKey:@"userid"],ApplicationDelegate.deviceID]]};
    NSLog(@"%@",dict);
    [AppNetwork requestWithPath:url parameters:parameters  success:^(id responseObject) {
        NSLog(@"%@",responseObject);
        NetworkModel *network = [NetworkModel mj_objectWithKeyValues:responseObject];
        NSInteger code = network.code.integerValue;
        if (code == 0){
            if ([network.dataObject isKindOfClass:[NSArray class]]){
                [self.dataArr removeAllObjects];
                NSArray *arr = [DynamicTrainModel mj_objectArrayWithKeyValuesArray:network.dataObject];
                [self.dataArr addObjectsFromArray:arr];
                NSLog(@"%ld",(unsigned long)self.dataArr.count);
                [self.tableView reloadData];
            }
            
        }else{
            [CustomProgressHUD showErrorWithStatus:network.errorDescription];
        }
    } failure:^(id error) {
        [CustomProgressHUD showErrorWithStatus:k_NetWorkStatus_Error];
    }];
}



@end

